{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#encoding:utf8\n",
    "from __future__ import print_function\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pylab as plt\n",
    "\n",
    "import keras\n",
    "from keras.datasets import cifar10\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense,Activation,Flatten,Dropout\n",
    "from keras.layers import Conv2D,MaxPool2D\n",
    "from keras.optimizers import RMSprop,SGD\n",
    "from keras.models import load_model\n",
    "from keras.models import model_from_json\n",
    "import scipy.io as sio\n",
    "\n",
    "from flask import Flask, render_template, request\n",
    "from scipy.misc import imsave, imread, imresize\n",
    "import re\n",
    "import sys\n",
    "import os\n",
    "from keras.preprocessing import image\n",
    "from keras.applications.vgg16 import VGG16\n",
    "from keras.applications.resnet50 import ResNet50\n",
    "from keras.applications import xception\n",
    "from keras.applications import inception_v3\n",
    "from keras.applications.vgg16 import preprocess_input, decode_predictions\n",
    "from keras.applications.resnet50 import preprocess_input, decode_predictions\n",
    "from keras.models import Model\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# model =  Model(input=base_model.input)\n",
    "# Model(input=base_model.input,outputs=base_model.get_layer('avg_pool').output)\n",
    "from keras.models import Model\n",
    "from keras.layers import Input, Dense\n",
    "base_model = ResNet50(weights='imagenet',include_top=False)\n",
    "for layer in base_model.layers:\n",
    "    layer.trainable = False\n",
    "\n",
    "base_model_out = base_model.output\n",
    "my_layer = Flatten()(base_model_out)\n",
    "my_layer = Dense(256,activation='relu')(my_layer)\n",
    "\n",
    "# model = Sequential()\n",
    "# model.add(base_model)\n",
    "# model.add(Flatten())\n",
    "# model.add(Dense(256,activation='relu'))\n",
    "\n",
    "# model.add(Dense(1,activation='sigmoid'))\n",
    "# model = Sequential() \n",
    "# https://stackoverflow.com/questions/43432717/keras-logistic-regression-returns-nan-on-first-epoch\n",
    "#https://stackoverflow.com/questions/43086548/how-to-manually-specify-class-labels-in-keras-flow-from-directory\n",
    "# Update: I ended up extending the DirectoryIterator class for the multilabel case. \n",
    "# You can now set the attribute \"class_mode\" to the value \"multilabel\" \n",
    "# and provide a dictionary \"multlabel_classes\" which maps filenames to their\n",
    "#  labels. Code: https://github.com/tholor/keras/commit/29ceafca3c4792cb480829c5768510e4bdb489c5\n",
    "# model.add(Dense(input_dim=1, activation='sigmoid',\n",
    "#             bias_initializer='normal', units=5))\n",
    "\n",
    " \n",
    "model = Dense(input_dim=1, activation='sigmoid',\n",
    "            bias_initializer='normal', units=5)(my_layer) \n",
    "rms = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)\n",
    "# learning rate should be low, as Resnet is ok\n",
    "# model.compile(optimizer=rms, loss='categorical_crossentropy')\n",
    "\n",
    "# model.compile(optimizer='rmsprop',\n",
    "#http://www.datalearner.com/blog/1051521451493989\n",
    "#在多标签分类中，大多使用binary_crossentropy损失而不是通常在多类分类中使用\n",
    "# 的categorical_crossentropy损失函数。这可能看起来不合理，但因为每个输出节点都是独立的\n",
    "# ，选择二元损失，并将网络输出建模为每个标签独立的bernoulli分布。\n",
    "model.compile(optimizer=rms ,\n",
    "            loss='binary_crossentropy',\n",
    "            metrics=['accuracy'])\n",
    "for i,layer in enumerate(model.layers):\n",
    "    print(i,layer.name)\n",
    "\n",
    "from keras.preprocessing.image import ImageDataGens\n",
    "train_data_gen = ImageDataGens(\n",
    "      preprocessing_function=preprocess_input,\n",
    "      rescale=1./255,\n",
    "      rotation_range=30,\n",
    "      width_shift_range=0.2,\n",
    "      height_shift_range=0.2,\n",
    "      shear_range=0.2,\n",
    "      zoom_range=0.2,\n",
    "      horizontal_flip=True\n",
    ")\n",
    "\n",
    "test_data_gen = ImageDataGens(\n",
    "      preprocessing_function=preprocess_input,\n",
    "      rescale=1./255,\n",
    "      rotation_range=30,\n",
    "      width_shift_range=0.2,\n",
    "      height_shift_range=0.2,\n",
    "      shear_range=0.2,\n",
    "      zoom_range=0.2,\n",
    "      horizontal_flip=True\n",
    ")\n",
    "train_dir = original_path = \"../../../Dataset/miml-image-data/original/miml_train_data\"\n",
    "test_dir = original_path = \"../../../Dataset/miml-image-data/original/miml_test_data\"\n",
    "\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "classes = ['desert','mountains','sea','sunset','trees']\n",
    "#https://stackoverflow.com/questions/43086548/how-to-manually-specify-class-labels-in-keras-flow-from-directory\n",
    "# Update: I ended up extending the DirectoryIterator class for the multilabel case. \n",
    "# You can now set the attribute \"class_mode\" to the value \"multilabel\" \n",
    "# and provide a dictionary \"multlabel_classes\" which maps filenames to their\n",
    "#  labels. Code: https://github.com/tholor/keras/commit/29ceafca3c4792cb480829c5768510e4bdb489c5\n",
    "#keras解决多标签分类问题http://www.datalearner.com/blog/1051521451493989\n",
    "train_generator = train_data_gen.flow_from_directory(\n",
    "        train_dir,                                               \n",
    "        target_size=(150, 150),                                  \n",
    "        batch_size=20,\n",
    "        classes =classes,\n",
    "        class_mode='categorical',\n",
    "        )                                     \n",
    "# https://blog.csdn.net/u012193416/article/details/79368855\n",
    "# 使用flow_from_directory最值得注意的是directory这个参数：\n",
    "# directory: path to the target directory. It should contain one subdirectory per class. Any PNG, JPG, BMP, PPM or TIF images inside each of the subdirectories directory tree will be included in the generator. \n",
    "# 这是官方文档的定义，它的目录格式一定要注意是包含一个子目录下的所有图片这种格式，driectoty路径只要写到标签路径上面的那个路径即可。\n",
    "# https://blog.csdn.net/weiwei9363/article/details/78635674\n",
    "test_generator = test_data_gen.flow_from_directory(\n",
    "        test_dir,\n",
    "        target_size=(150, 150),\n",
    "        batch_size=20,\n",
    "        classes = classes,\n",
    "        class_mode='categorical')\n",
    "\n",
    "def preprocess_img(img_path):\n",
    "    img = image.load_img(img_path,target_size=(224,224))\n",
    "    x = image.img_to_array(img)\n",
    "    x = np.expand_dims(x,axis = 0)\n",
    "    x = preprocess_input(x)\n",
    "    return x\n",
    "def predict_top_labels(preds,top_num):\n",
    "    print('Predicted:', decode_predictions(preds, top=top_num)[0])\n",
    "    return\n",
    "\n",
    "\n",
    "\n",
    "# history = model.fit_generator(\n",
    "#       train_generator,\n",
    "#       steps_per_epoch=100,\n",
    "#       epochs=30)\n",
    "\n",
    "# model.save(args.output_model_file)\n",
    "\n",
    "# a = Input(shape=(32,))\n",
    "# b = Dense(32)(a)\n",
    "# model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output)\n",
    "\n",
    "# img_path = \"../pics/woodgirl.jpg\"\n",
    "# x = preprocess_img(img_path)\n",
    "# preds = model.predict(x)\n",
    "# predict_top_labels(preds,3)\n",
    "# history = model.fit(X, Y_oh, \n",
    "#     batch_size=batch_size, \n",
    "#     epochs=nb_epoch)\n",
    "\n",
    "# https://www.cnblogs.com/skyfsm/p/8051705.html\n",
    "# 读数据"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
